package com.tengju.user.infrastructure.repository.csa;

import com.alibaba.fastjson.JSONArray;
import com.tengju.user.domain.model.csa.ClubCustomerServiceInfo;
import com.tengju.user.domain.model.csa.GenSecCustomerServiceInfoRepository;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.GenSecCustomerServiceInfoDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.GenSecCustomerServiceInfoDOMapper;
import com.tengju.user.infrastructure.repository.convert.GenSecCustomerserviceInfoConvert;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Repository
public class GenSecCustomerServiceInfoRepositoryImpl implements GenSecCustomerServiceInfoRepository {

    @Resource
    GenSecCustomerServiceInfoDOMapper genSecCustomerServiceInfoDOMapper;

    @Override
    public ClubCustomerServiceInfo getBySecIdCode(UserIdCode secIdCode) {
        if (secIdCode == null) {
            return null;
        }
        GenSecCustomerServiceInfoDO genSecCustomerServiceInfoDO = genSecCustomerServiceInfoDOMapper.getBySecIdCode(secIdCode.getValue());
        return GenSecCustomerserviceInfoConvert.convertClubCsaInfoDOToCInfo(genSecCustomerServiceInfoDO);
    }

    @Override
    public int updateCustomerServiceInfo(UserIdCode secIdCode, String csaName, String csaWechat,
                                         String headImage, Integer workDateType, LocalDateTime startWorkTime,
                                         LocalDateTime endWorkTime, JSONArray customerServiceGreetJson) {
        return genSecCustomerServiceInfoDOMapper.updateCustomerServiceInfo(secIdCode.getValue(),csaName,
                csaWechat,headImage,workDateType,startWorkTime,endWorkTime,customerServiceGreetJson.toJSONString());

    }

    @Override
    public long addCustomerServiceInfo(UserIdCode secIdCode, String csaName, String csaWechat,
                                       String headImage,Integer workDateType,LocalDateTime startWorkTime,
                                       LocalDateTime endWorkTime,JSONArray customerServiceGreetJson) {
        long count = genSecCustomerServiceInfoDOMapper.insert(secIdCode.getValue(),csaName,
                csaWechat,headImage,workDateType,startWorkTime,endWorkTime,customerServiceGreetJson.toJSONString());
        return count;
    }

    @Override
    public void clearCustomerServiceInfo(UserIdCode secIdCode) {
        GenSecCustomerServiceInfoDO genSecCustomerServiceInfoDO = genSecCustomerServiceInfoDOMapper.getBySecIdCode(secIdCode.getValue());
        if (genSecCustomerServiceInfoDO != null) {
            genSecCustomerServiceInfoDOMapper.clearCustomerServiceInfo(secIdCode.getValue());
        }
    }
}

